package com.slicktechnologies.server.addhocdownload;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CreateCSVServlet extends HttpServlet {
	
	 
	private static final long serialVersionUID = 1L;
	


	public static final int CUSTOMER=1,QUOTATION=2,Contract=6,LEADS=3,EXPENSES=4,SERVICE=5,

			CALENDAR=7,LEAVEAPPLICATION=8,ADVANCE=9,COMPANY=20,EMPLOYEESHIFT=10,LEAVEBALANCE=11,TOOLSET=22,TOOL=23,CLIENTSIDEASSET=24
			,CTC=30,EMPLOYEE=40;
            


	HttpServletResponse responseref;
	PrintWriter pw;
	private SimpleDateFormat fmt= new SimpleDateFormat("dd-MMM-yyyy");
	@Override
	 protected void doGet(HttpServletRequest request,
	   HttpServletResponse response) throws ServletException, IOException 
	  {
	  
		
		response.setContentType("application/csv");
		 responseref=response;
		 
	     pw=response.getWriter();    
	     String reqtype=request.getParameter("type");
	     
	     System.out.println("Type is "+reqtype);
	     int type=Integer.parseInt(reqtype);
	     getcsv(type);
	     
	     pw.flush();
	     pw.close();
	  }
	
	public void getcsv(int type) throws IOException
	{
		 CsvWriter csv=new CsvWriter();
		 Date d=new Date();
		 String dateString = fmt.format(d);
	
		switch(type)
		{
			case CUSTOMER:
				            responseref.setHeader("Content-Disposition", "Attachment;filename=Customer Report "+dateString+".csv"
				            		);
							csv.getCustomerCSV(pw);
							break;
							
							
			case QUOTATION:
				 responseref.setHeader("Content-Disposition", "Attachment;filename=Quotation/Contract Report "+dateString+".csv"
		            		);
							csv.getQuotationCSV(pw);
							break;
				
			
			
			case Contract:
				responseref.setHeader("Content-Disposition", "Attachment;filename=Service Card Report "+dateString+".csv"
	            		);
						csv.getServiceCardCSV(pw);
						break;

			case COMPANY:
				responseref.setHeader("Content-Disposition", "Attachment;filename=Registered Company Report "+dateString+".csv"
	            		);
						csv.getCompanyCSV(pw);
						break;

			case EMPLOYEE:
							responseref.setHeader("Content-Disposition", "Attachment;filename=Leave Application Report "+dateString+".csv");
							csv.getEmployeeCSV(pw);
							break;
				
		}
							
	}

	
}


